﻿using Com.Lancetinc.Evs.Helpers;
using Com.Lancetinc.Evs.Services;
using MahApps.Metro.IconPacks;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace Com.Lancetinc.Evs.Controls
{
    /// <summary>
    /// 视频播放控件
    /// </summary>
    public partial class ExperVideoPreviewUserControl
    {
        private ExperInfo mExper;

        public event EventHandler<bool> FullScreenChanged;

        private bool _isFullScreen = false;
        public bool IsFullScreen
        {
            get => _isFullScreen;
            set
            {
                _isFullScreen = value;
                OnFullScreenChanged(value);
            }
        }

        public event EventHandler RequireShowCapturedScreen;

        public event EventHandler<string> VideoImageCaptured;

        public void SetExper(ExperInfo exper)
        {
            if (mExper != null)
            {
                mExper.Images.CollectionChanged += Images_CollectionChanged;
            }
            mExper = exper;
            CtrlMedia.SetExper(exper);

            mExper.Images.CollectionChanged += Images_CollectionChanged;
            Images_CollectionChanged(this, null);
            var fi = new FileInfo(mExper.VideoPath);
            // 文件不要小于2K
            BtnStartPause.IsEnabled = fi.Exists && fi.Length > 2048;
            if (!BtnStartPause.IsEnabled)
            {
                BtnFullScreen.IsEnabled = false;
            }
        }

        private void Images_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            var imgCnt = mExper.Images.Count;
            LblCapturedScreen.Text = I18nHelper.Default.GetMessageWithArgs("Button.CapturedImage", imgCnt);
            BtnCapturedScreen.IsEnabled = imgCnt > 0;
        }

        public ExperVideoPreviewUserControl()
        {
            InitializeComponent();
            CtrlMedia.VideoStateChanged += ctrlMedia_VideoStateChanged;
            LblCapturedScreen.Text = I18nHelper.Default.GetMessageWithArgs("Button.CapturedImage", 0);
            this.SizeChanged += ExperVideoPreviewUserControl_SizeChanged;
        }

        private bool _isButtonLayoutNarrow = false;
        private const double NarrowButtonThreshold = 560;
        private void ExperVideoPreviewUserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            if (e.NewSize.Width < NarrowButtonThreshold && !_isButtonLayoutNarrow)
            {
                ApplyButtonNarrowStyle();
                _isButtonLayoutNarrow = true;
            }
        }

        /// <summary>
        /// 在报告页面，左边元素比较多，在小屏上按钮条空间不足,因此这里减少各按钮的宽度
        /// </summary>
        private void ApplyButtonNarrowStyle()
        {
            ButtonBorder.Margin = new Thickness(0, ButtonBorder.Margin.Top, 0, ButtonBorder.Margin.Bottom);

            ButtonPanel.Children.OfType<Button>()
                .ToList()
                .ForEach(x => x.MinWidth = 0);
        }

        private void ctrlMedia_VideoStateChanged(object sender, VideoStateChangeEventArgs e)
        {
            BtnStartPause.IsEnabled = File.Exists(mExper.VideoPath);

            if (e.VideoState == VideoStateEnum.Playing)
            {
                LblStartPause.Text = I18nHelper.Default.GetMessage("Button.Pause");
                IcStartPause.Kind = PackIconLevsKind.Pause;
                BtnCaptureScreen.IsEnabled = true;
            }
            else if (e.VideoState == VideoStateEnum.Paused)
            {
                LblStartPause.Text = I18nHelper.Default.GetMessage("Button.Play");
                IcStartPause.Kind = PackIconLevsKind.VideoPlay;
                BtnCaptureScreen.IsEnabled = true;
            }
            else
            {
                LblStartPause.Text = I18nHelper.Default.GetMessage("Button.Play");
                IcStartPause.Kind = PackIconLevsKind.VideoPlay;
                BtnCaptureScreen.IsEnabled = false;
            }
        }

        private void BtnStartPause_Click(object sender, RoutedEventArgs e)
        {
            if (CtrlMedia.VideoState == VideoStateEnum.Playing)
            {
                CtrlMedia.Pause();
            }
            else
            {
                CtrlMedia.Play();
            }
        }

        private void BtnFullScreen_Click(object sender, RoutedEventArgs e)
        {
            this.IsFullScreen = !this.IsFullScreen;
        }

        private void OnFullScreenChanged(bool isFullScreen)
        {
            LblFullScreen.Text = isFullScreen
                ? Msgs.Button_ExitFullScreen
                : Msgs.Button_EnterFullScreen;
            CtrlMedia.IsFullScreen = isFullScreen;

            FullScreenChanged?.Invoke(this, isFullScreen);
        }

        private void BtnCaptureScreen_Click(object sender, RoutedEventArgs e)
        {
            string imgName = DoCaptureVideoImage();
            VideoImageCaptured?.Invoke(this, imgName);
        }

        private void BtnCapturedScreen_Click(object sender, RoutedEventArgs e)
        {
            RequireShowCapturedScreen?.Invoke(this, EventArgs.Empty);
        }

        private string DoCaptureVideoImage()
        {
            string imageName = ExperManager.Default.CreateImageName(mExper.GetStartTime().AddMilliseconds(CtrlMedia.PlayedMillSeconds));
            var path = mExper.GetImageFullPath(imageName);
            CtrlMedia.CaptureImage(path);
            if (!mExper.Images.Contains(imageName))
            {
                mExper.Images.Add(imageName);
            }

            return imageName;
        }

        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            if (mExper != null)
            {
                mExper.Images.CollectionChanged -= Images_CollectionChanged;
            }
        }
    }
}
